package com.financing.wap.web.controller.api;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.Session;
import com.financing.wap.dto.FpShopBackgroundDTO;
import com.financing.wap.dto.FpShopUserBackgroundDTO;
import com.financing.wap.dto.ShopperUserInfoDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IFpShopBackgroundService;
import com.financing.wap.service.IFpShopUserBackgroundService;
import com.financing.wap.service.IFpShopperProductService;
import com.financing.wap.service.IShopperUserInfoService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("fpShopBackgroundController")
@RequestMapping("/api/fpShopBackground")
public class FpShopBackgroundController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpShopBackgroundService fpShopBackgroundService;
	@Autowired
	private IFpShopUserBackgroundService fpShopUserBackgroundService;
	
	@Autowired
	private IShopperUserInfoService shopperUserInfoService;
	
	
	@RequestMapping("/listFpShopBackground")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpShopBackground list begin .......");
			
//			fpShopBackground.setBegin(pager.getStartIndex());
//			fpShopBackground.setPageSize(pager.getPageSize());
//			int count = fpShopBackgroundService.queryListCount(fpShopBackground);
//			pager.setTotalCount(count);
//			
			//查询用户历史
			Session session = getSession(true);
			UserDTO loginUser = session.getUser();
			
			
			FpShopUserBackgroundDTO fpShopUserBackground = new FpShopUserBackgroundDTO();
			fpShopUserBackground.setLoginName(loginUser.getLoginName());
			List<FpShopUserBackgroundDTO> userBack = new ArrayList<FpShopUserBackgroundDTO>();  
			userBack =fpShopUserBackgroundService.queryList(fpShopUserBackground);
			if(userBack.size()==0){
				//查询现有店招 
				FpShopUserBackgroundDTO bb = new FpShopUserBackgroundDTO();
				bb.setBackgroudImg("");
				userBack.add(bb);
				userBack.add(bb);
				ShopperUserInfoDTO shopinfo = new ShopperUserInfoDTO();
				shopinfo.setUserPhone(loginUser.getLoginName());
				List<ShopperUserInfoDTO> shops= shopperUserInfoService.queryList(shopinfo);
				if(shops.size()==1 && StringUtils.isNotBlank(shops.get(0).getShopBackground())){
					FpShopUserBackgroundDTO bbs = new FpShopUserBackgroundDTO(); 
					bbs.setBackgroudImg(shops.get(0).getShopBackground());
					userBack.set(0, bbs);
				}
				
				
			}else if(userBack.size()==1){
				FpShopUserBackgroundDTO bb = new FpShopUserBackgroundDTO();
				bb.setBackgroudImg("");
				userBack.add(bb);
			}
			
			
			
			
			fpShopBackground.setType(1); //查询实时
			List<FpShopBackgroundDTO> news = fpShopBackgroundService.queryList(fpShopBackground);
			
			fpShopBackground.setType(2); //查询固定
			List<FpShopBackgroundDTO>  langs= fpShopBackgroundService.queryList(fpShopBackground);	
			
			result.setAttribute("pager", pager);
			result.setAttribute("news", news);
			result.setAttribute("langs", langs);
			result.setAttribute("userBack", userBack);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpShopBackground list end .......");
		}catch(Exception e){
			e.printStackTrace();
			log.error("query fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpShopBackground")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpShopBackground  begin .......");
			
			DTOdateSetUtil.setCreateInfo(fpShopBackground);
			DTOdateSetUtil.setUpdateInfo(fpShopBackground);
			
			int id = fpShopBackgroundService.insert(fpShopBackground);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpShopBackground list end .......");
		}catch(Exception e){
			log.error("add fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpShopBackground")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpShopBackground  begin .......");
			
			Integer id = fpShopBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpShopBackgroundDTO dto = fpShopBackgroundService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpShopBackground  end .......");
		}catch(Exception e){
			log.error("query detail fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpShopBackground")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpShopBackground  begin .......");
			
			Integer id = fpShopBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpShopBackgroundService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpShopBackground  end .......");
		}catch(Exception e){
			log.error("delete fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpShopBackground")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpShopBackground  begin .......");
			
			Integer id = fpShopBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpShopBackground);
			
			fpShopBackgroundService.update(fpShopBackground);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpShopBackground list end .......");
		}catch(Exception e){
			log.error("update fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpShopBackground")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpShopBackgroundDTO fpShopBackground){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpShopBackground  begin .......");
			
			Integer id = fpShopBackground.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(fpShopBackground);
			
			fpShopBackgroundService.updateForce(fpShopBackground);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpShopBackground list end .......");
		}catch(Exception e){
			log.error("updateForce fpShopBackground error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
